<!--
/**
 * @name 'dataStatisticsView'
 * @desc ''
 * @author ''
 * @time 2022年01月17日 11:42:38 星期一
 */
-->
<template>
  <div class="scrollbar routerview scroll-box">
      <div class="innerBox">
        <div class="main-box">
          <div class="top-box">
            <div class="left-box" style="flex:25%">
              <div class="box-card" style="min-width:220px">
                  <el-avatar :size='70' style="font-size:30px">{{Name.slice(0,1)}}</el-avatar>
                  <span class="un-box">
                    <h2>{{Name}}</h2>
                    <p style="font-size:14px;color:#999" v-if="roleName!==''">{{roleName}}</p>
                  </span>
              </div>
            </div>
            <div class="right-box" style="flex:75%">
            <div class="data-box">
              <div class="col">
                <div class="col1">
                  <i class="el-icon-box col-icon icon1"></i>
                  <div class="col-text">
                    <div class="col-num1 col-num">{{ count1 }}</div>
                    <div>库存总量</div>
                  </div>
                </div>
              </div>
              <div class="col">
                 <div class="col1">
                  <i class="el-icon-takeaway-box col-icon icon2"></i>
                  <div class="col-text">
                     <div class="col-num2 col-num">{{wareCount}}</div>
                    <div>仓库数量</div>
                  </div>
                </div>
              </div>
              <div class="col col-end">
                 <div class="col1">
                  <i class="el-icon-receiving col-icon icon3"></i>
                  <div class="col-text">
                     <div class="col-num1 col-num">{{wareSpace}}</div>
                    <div>仓库容量</div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          </div>
          <div class="top-box">
            <div class="left-box" style="flex:30%">
              <div class="box-card1 box-card" style="display:block;height:280px">
                <p class="title">
                  <i class="el-icon-s-home" style="color:#64d572"></i>
                  仓库容量</p>
                <div class="scrollbar content-box" v-loading="loading">
                  <ul v-if="!loading">
                    <li v-for="(item,index) in wareHouseList" :key="item.objectId">
                       <div class="content-item">
                          <span style="flex:1">
                          <!-- <el-avatar :src="ranking(index)" style="margin-top:2px" v-if="ranking(index)"></el-avatar> -->
                            <p style="font-weight:500">{{item.name}}</p>
                          </span>
                          <span style="flex:4;margin-left:10px;margin-top:2px">
                          <!-- <p style="font-weight:500">{{item.name}}</p> -->
                          <el-progress :text-inside="true" :stroke-width="20" :status="status(item.storageTotal,item.upperlimit)" 
                          :percentage="computedSpace(item.storageTotal,item.upperlimit)">
                          </el-progress>
                          </span>
                         
                       </div>
                    </li>
                  </ul>
                 <h3 class="text-center"  v-if="!loading&&errorStatus">加载失败
                    <i class="el-icon-refresh-right pointer" style="margin-left:5px;font-weight:600;padding-top:4px;" 
                    @click="getRankingData"
                    ></i>
                  </h3>
                </div>
              </div>
            </div>
            <div class="right-box" style="flex:70%">
              <div class="box-card1 box-card" style="display:block;height:280px">
                <p class="title">
                  <i class="el-icon-s-marketing" style="color:#f25e43"></i>
                  出入库统计</p>
                <div class="scrollbar content-box" v-loading="loading">
                    <div class="item-box">
                      <div>
                        <h3 style="color:#64d572">{{stockCount}}</h3>
                        <div>入库单量</div>
                      </div>
                      <div>
                        <h3 style="color:#2d8cf0">{{count1}}</h3>
                        <div>入库件量</div>
                      </div>
                      <div>
                        <h3 style="color:#808080">{{count2}}</h3>
                        <div>合格数量</div>
                      </div>
                      <div>
                        <h3 style="color:#ff8000">{{count3}}</h3>
                        <div>不合格量</div>
                      </div>
                    </div>

                     <div class="item-box">
                      <div>
                        <h3 style="color:#64d572">{{OutstockCount}}</h3>
                        <div>出库单量</div>
                      </div>
                      <div>
                        <h3 style="color:#2d8cf0">{{count4}}</h3>
                        <div>出库件量</div>
                      </div>
                      <div>
                        <h3 style="color:#808080">{{count5}}</h3>
                        <div>合格数量</div>
                      </div>
                      <div>
                        <h3 style="color:#ff8000">{{count6}}</h3>
                        <div>不合格量</div>
                      </div>
                    </div>
                 <h3 class="text-center"  v-if="!loading&&errorStatus">加载失败
                    <i class="el-icon-refresh-right pointer" style="margin-left:5px;font-weight:600;padding-top:4px;" 
                    @click="getRankingData"
                    ></i>
                  </h3>
                </div>
              </div>
            </div>
          </div>
          <div class="piechart-box" v-if="show">
            <div class="box-chart">
               <lineCharts :data='stockList' :outStockData='OutstockList'></lineCharts>
            </div>
          </div>

          <div class="piechart-box" v-if="show">
            <div class="box-chart">
               <lineChartsA :data='stockList' :outStockData='OutstockList'></lineChartsA>
            </div>
          </div>
          
           <div class="piechart-box" v-if="show">
            <div class="box-chart">
               <pieCharts :data='stockList'></pieCharts>
            </div>
          </div>
        
     
          
        </div>
      </div>
      <el-backtop target=".routerview">
      </el-backtop>
      <el-backtop target=".routerview" :bottom="100" >
        <div style="width:100%;height:100%;background-color: #ffffff;text-align:center;border-radius: 50%;line-height: 40px;">
           <i class="el-icon-refresh" @click="getData"></i>
        </div>
      </el-backtop>
  </div>
</template>

<script>
import lineCharts from '@/components/echarts/lineCharts'
import lineChartsA from '@/components/echarts/lineChartsA'
import pieCharts from '@/components/echarts/pieCharts'
export default {
  name: 'dataStatisticsView',
  components: { lineCharts , lineChartsA ,pieCharts},
  props: {},
  data () {
    return {
      roleName:'',
      wareSpace:0,
      wareCount:0,
      stockCount:0,
      OutstockCount:0,
      loading:true,
      errorStatus:false,
      wareHouseList:[],
      stockList:[],
      OutstockList:[],
      show:false
     

    }
  },
  computed: {
    computedSpace(){
      return function(total,current){
        let percentage = total / 100;
        return Number((current / percentage).toFixed(2))
      }
    },
    status(){
      return function(total,current){
        let percentage = total / 100;
        if(Number((current / percentage).toFixed(2)) > 80){
          return 'success'
        }else if(Number((current / percentage).toFixed(2)) > 60){
          return null
        }else if(Number((current / percentage).toFixed(2)) > 30){
          return 'warning'
        }else{
          return 'exception'
        }
      }
    },
    count1(){
      let sum = 0
      this.stockList.forEach(item => {
        sum=sum+item.amount;
      });
      return sum
    },
    count2(){
      let sum = 0
      this.stockList.forEach(item => {
        if(item.status){
          sum++;
        }
      });
      return sum
    },
    count3(){
      let sum = 0
      this.stockList.forEach(item => {
        if(!item.status){
          sum++;
        }
      });
      return sum
    },
    count4(){
      let sum = 0
      this.OutstockList.forEach(item => {
        sum=sum+item.amount;
      });
      return sum
    },
    count5(){
      let sum = 0
      this.OutstockList.forEach(item => {
        if(item.stock.status){
          sum++;
        }
      });
      return sum
    },
    count6(){
      let sum = 0
      this.OutstockList.forEach(item => {
        if(!item.stock.status){
          sum++;
        }
      });
      return sum
    }
     
  },
  watch: {},
  created () {
   this.getData()
  },
  mounted () {},
  methods: {
    async getData(){
      this.getRoleData()
      await this.getWareHouseData()
      await this.getStockData()
      await this.getOutStockData()
      this.loading=false
      this.show=true
    },

    getRoleData(){
      const query = this.$Bmob.Query("Role");
      query.get(this.RoleId).then(res => {
        this.roleName = res.roleName
      }).catch(err=>{
      })
    },
    getWareHouseData(){
       return this.$http.getAllData(this,'wareHouse').then(data=>{
            //成功回调
            this.wareHouseList=data
           this.wareCount=data.length
           data.forEach(item => {
             this.wareSpace+=item.upperlimit
           });
        }).catch(error=>{
            //失败回调
          this.loading=false
          this.errorStatus=true
        })
    },
    getStockData(){
    
      const query = this.$Bmob.Query('Stock')
      // query.include('parts','edit_operator','operator','storage','source')
      query.select('amount','status')
      query.limit(9999)
      return query.find().then(res=>{
        this.stockCount=res.length
        this.stockList=res
      }).catch(err=>{
        this.show=true
        this.loading=false
        this.errorStatus=true
      })
    },
    getOutStockData(){
      const query = this.$Bmob.Query('OutStock')
      // query.order('-createdAt')
      query.include('parts','operator','stock','toEnd','stock.source','stock.storage','stock.parts')
      // query.select('amount')
      query.limit(9999)
      return query.find().then(res=>{
         this.OutstockCount=res.length
         this.OutstockList = res
      }).catch(err=>{
        this.show=true
        this.loading=false
        this.errorStatus=true
      })
    
    }
  },
}

</script>
<style lang='scss' scoped>
$blue:#2d8cf0;
$green:#64d572;
$red:#f25e43;
$grey:#ebedf0;

*{
  box-sizing: border-box;
}

.scroll-box {
  overflow-x: auto;
}

.main-box {
  width: 100%;
}
.top-box {
  margin-bottom: 20px;
  display: flex;
}
.left-box {
  padding: 8px;
  flex: 50%;
  
}
.right-box {
  flex: 50%;
  padding: 8px;
}
.left-chartbox {
  flex: 50%;
  padding: 8px;
}
.right-chartbox {
  flex: 50%;
  padding: 8px;
}
.box-card {
    width: 100%;
    border-radius: 10px;
    padding: 20px;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    background-color: white;
    box-shadow: 0 2px 12px 0 $grey;
}
.box-chart {
    width: 100%;
    border-radius: 10px;
    padding: 20px 20px 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: white;
    box-shadow: 0 2px 12px 0 $grey;
    margin-bottom: 20px;
    // min-width: 700px;
}
.piechart-box {
  flex: 100%;
  padding: 8px;
  min-width: 1000px;
}


.un-box {
  margin-left: 20px;
}
.title {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 15px;
  min-width: 400px;
}

.data-box {
  // margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  align-content: stretch;

  

  .col {
    padding: 0 12px 0 0;
    flex: 1;

    .col1 {
      display: flex;
      align-items: center;
      justify-content: flex-start;
      height: 110px;
      background-color: white;
      border-radius: 10px;
      box-shadow: 0 2px 12px 0 $grey;
      

      .col-icon {
        font-size: 50px;
        width: 100px;
        height: 110px;
        text-align: center;
        line-height: 110px;
        color: #fff;
        border-top-left-radius: 10px;
        border-bottom-left-radius: 10px;
        
      }
      .col-text {
        flex: 1;
        width: 120px;
        text-align: center;
        font-size: 14px;
        color: #999;

      .col-num {
        font-size: 30px;
        font-weight: 700
      }

      }
    }
  }

}
.data-box .col-end {
    padding: 0px;
  }

.icon1 {
  background: $blue;
}

.icon2 {
  background: $green;
}

.icon3 {
  background-color: $red;
}

.col-num1 {
  color: $blue;
}
.col-num2 {
  color: $green;
}
.col-num3 {
  color: $red;
}

.box-card1 {
  align-items: flex-start;
  display: block;
  padding: 20px 10px 20px 20px;

  .el-card__header {
    padding: 18px 20px 18px 0;
    border-bottom: 1px solid $grey;
    width: 100%;

    .header-box {
      border-left: 6px solid $green;
    }
}
  .list-box {
    padding: 10px 20px;

    .list-box-content {
      padding: 15px 0;
      border-bottom: 1px solid $grey;

    }
  }
}

.feedback-content {
  font-weight: 500;
  font-size: 14px;
  color: black;
  text-justify:inter-ideograph;
  text-align:justify;
  margin-bottom: 20px;
}
.feedback-footer {
  color: #808080;
  font-size: 13px;
  font-weight: 400;
  text-align: right;
  margin-top: 10px;
  
}
  
.content-box {
  height: 280px;
  overflow: auto;
  padding: 0 10px;
}

.content-item {
  margin: 5px 0;
  padding: 15px;
  display: flex;
  align-items: center;
}

.item-box {
  display: flex;
  margin: 5px 5px 10px;
  padding: 20px;
  border-radius: 15px;
  background-color: #f1f1f1;
  display: flex;
  align-items: center;
  justify-content: space-around;
  text-align: center;
  font-size: 14px;

  h3 {
    font-size: 20px;
    margin-bottom: 5px;
  }
  div {
    flex:1;
  }
}

.text-center {
  height: 100%;
  opacity: 0.4;
  display: flex;
  justify-content: center;
  align-items: center;
}

/deep/ .el-card {
  border-radius: 10px;
}


.content-rate {
  display: flex;
  align-items: flex-start;
  font-size: 13px;
  margin: 5px 0px;
}
</style>
